﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastExecutor.ORG.DB
{
    public class DB_Module : FastTemplateDB<Frame_Module>
    {
        public override void Insert(Frame_Module Entity)
        {
            Entity.RowGuid = CommonUtil.CreateCommonGuid();
            Entity.AddDate = DateTime.Now;
            Entity.IsEnable = 10;
            Entity.ModuleCode = DataBuilder.CreateTableFieldCodeValue("Frame_Module", "RowGuid", "ModuleCode", "ParentModuleGuid", Entity.ParentModuleGuid);
            base.Insert(Entity);
        }

        public override void Update(Frame_Module Entity)
        {
            Frame_Module DBEntity = FindOne(Entity.RowGuid);
            if (DBEntity.ParentModuleGuid != Entity.ParentModuleGuid)
            {
                Entity.ModuleCode = DataBuilder.CreateTableFieldCodeValue("Frame_Module", "RowGuid", "ModuleCode", "ParentModuleGuid", Entity.ParentModuleGuid);
            }
            base.Update(Entity);
        }

        public override void Update(Newtonsoft.Json.Linq.JObject JsonEntity)
        {
            Frame_Module DBEntity = FindOne(Convert.ToString(JsonEntity.GetValue("RowGuid")));
            if (DBEntity.ParentModuleGuid != Convert.ToString(JsonEntity.GetValue("ParentModuleGuid")))
            {
                JsonEntity["ModuleCode"] = DataBuilder.CreateTableFieldCodeValue("Frame_Module", "RowGuid", "ModuleCode", "ParentModuleGuid", Convert.ToString(JsonEntity.GetValue("ParentModuleGuid")));
            }
            base.Update(JsonEntity);
        }

        public override void BatchUpdate(List<Frame_Module> EntityList)
        {
            foreach (Frame_Module Entity in EntityList)
            {
                Frame_Module DBEntity = FindOne(Entity.RowGuid);
                if (DBEntity.ParentModuleGuid != Entity.ParentModuleGuid)
                {
                    Entity.ModuleCode = DataBuilder.CreateTableFieldCodeValue("Frame_Module", "RowGuid", "ModuleCode", "ParentModuleGuid", Entity.ParentModuleGuid);
                }
            }
            base.BatchUpdate(EntityList);
        }

        public override void BatchUpdate(Newtonsoft.Json.Linq.JArray JsonArray)
        {
            foreach (JObject JsonEntity in JsonArray)
            {
                Frame_Module DBEntity = FindOne(Convert.ToString(JsonEntity.GetValue("RowGuid")));
                if (DBEntity.ParentModuleGuid != Convert.ToString(JsonEntity.GetValue("ParentModuleGuid")))
                {
                    JsonEntity["ModuleCode"] = DataBuilder.CreateTableFieldCodeValue("Frame_Module", "RowGuid", "ModuleCode", "ParentModuleGuid", Convert.ToString(JsonEntity.GetValue("ParentModuleGuid")));
                }
            }
            base.BatchUpdate(JsonArray);
        }

        public override void BatchDelete(List<string> KeyGuidList)
        {
            foreach (string KeyGuid in KeyGuidList)
            {
                @DBUtil.ExecuteSQL(string.Format("delete from Frame_ModuleRight where RowGuid='{0}'", KeyGuid));
            }
            base.BatchDelete(KeyGuidList);
        }

        public Frame_Module GetModuleWithUrl(string Url)
        {
            Frame_Module Entity = null;
            List<Frame_Module> list = base.FindAll(string.Format(" and ModuleUrl='{0}'", Url));
            if (list.Count>0)
            {
                Entity = list[0];
            }
            return Entity;
        }
    }
}
